package com.edu.exam.enums.examination;

import com.edu.exam.enums.core.BaseEnum;

/**
 * 时间控制状态枚举
 *
 * <p>定义考试时间控制系统的状态，包括未开始、运行中、暂停、已完成等状态。</p>
 *
 * @author System
 * @version 1.0.0
 * @since 2025-11-14
 */
public enum TimeControlState implements BaseEnum {

    /** 未开始 */
    NOT_STARTED("NOT_STARTED", "未开始", "时间控制尚未开始计时"),

    /** 运行中 */
    RUNNING("RUNNING", "运行中", "时间控制系统正在运行计时"),

    /** 暂停 */
    PAUSED("PAUSED", "暂停", "时间控制已暂停计时"),

    /** 已完成 */
    COMPLETED("COMPLETED", "已完成", "时间控制已结束"),

    /** 超时 */
    EXPIRED("EXPIRED", "超时", "时间控制已超过设定时间"),

    /** 异常终止 */
    TERMINATED("TERMINATED", "异常终止", "时间控制因异常而终止"),

    /** 手动停止 */
    MANUALLY_STOPPED("MANUALLY_STOPPED", "手动停止", "时间控制被手动停止");

    private final String code;
    private final String description;
    private final String detail;

    TimeControlState(String code, String description, String detail) {
        this.code = code;
        this.description = description;
        this.detail = detail;
    }

    @Override
    public String getCode() {
        return code;
    }

    @Override
    public String getDescription() {
        return description;
    }

    public String getDetail() {
        return detail;
    }

    @Override
    public String toString() {
        return this.code + "(" + this.description + ")";
    }

    /**
     * 根据编码查找枚举值
     *
     * @param code 业务编码
     * @return 对应的枚举值
     * @throws IllegalArgumentException 如果编码无效
     */
    public static TimeControlState fromCode(String code) {
        if (code == null || code.trim().isEmpty()) {
            throw new IllegalArgumentException("Time control state code cannot be null or empty");
        }

        for (TimeControlState state : values()) {
            if (state.getCode().equals(code)) {
                return state;
            }
        }
        throw new IllegalArgumentException("Unknown time control state code: " + code);
    }

    /**
     * 根据描述查找枚举值
     *
     * @param description 显示描述
     * @return 对应的枚举值
     * @throws IllegalArgumentException 如果描述无效
     */
    public static TimeControlState fromDescription(String description) {
        if (description == null || description.trim().isEmpty()) {
            throw new IllegalArgumentException("Time control state description cannot be null or empty");
        }

        for (TimeControlState state : values()) {
            if (state.getDescription().equals(description)) {
                return state;
            }
        }
        throw new IllegalArgumentException("Unknown time control state description: " + description);
    }
}